package com.bdqn.oa.service.impl;

import com.bdqn.oa.dao.AttendanceDao;
import com.bdqn.oa.dao.impl.AttendanceDaoImpl;
import com.bdqn.oa.entity.Attendance;
import com.bdqn.oa.service.AttendanceService;
import com.bdqn.oa.utils.DruidUtils;
import com.bdqn.oa.utils.DateUtils;
import com.bdqn.oa.utils.Page;
import java.sql.Connection;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author zhowei
 * @date 2022/7/20 15:11
 */
public class AttendanceServiceImpl implements AttendanceService {
    private AttendanceDao attendanceDao = new AttendanceDaoImpl();
    /**
     * 上班打卡
     *
     * @param attendance
     * @return
     */
    @Override
    public int insertMorningAtt(Attendance attendance) throws SQLException {
        Connection connection = null;
        int result= 0;
        try{  // 增加事物控制
            connection= DruidUtils.getConnection();
            connection.setAutoCommit(false);
            attendance.setDate(DateUtils.getLocalDateTime());
            result= attendanceDao.insertMorningAtt(attendance);
            if(result == 1){
                connection.commit();
            }else{
                connection.rollback();   // 上班打卡失败，事物回滚
            }
        }catch(Exception e){
            if(connection!=null) {
                connection.rollback();
            }
            e.printStackTrace();
        }finally {
            DruidUtils.close();
        }
        return result;
    }

    /**
     * 下班打卡，通过员工id查找上班打卡数据，新增下班打卡时间
     *
     * @param
     */
    @Override
    public int insertAfternoonTimeAtt(Attendance attendance) throws SQLException {
        Connection connection = null;
        int result= 0;
        try{  // 增加事物控制
            connection= DruidUtils.getConnection();
            connection.setAutoCommit(false);
            result= attendanceDao.insertAfternoonTimeAtt(attendance);
            if(result == 1){
                connection.commit();
            }else{
                connection.rollback();   // 下班打卡失败，事物回滚
            }
        }catch(Exception e){
            if(connection!=null) {
                connection.rollback();
            }
            e.printStackTrace();
        }finally {
            DruidUtils.close();
        }
        return result;
    }

    /**
     * 获取某员工的打卡数量
     *
     * @param id
     * @return
     * @throws SQLException
     */
    @Override
    public long countById(Integer id) throws SQLException {
        return attendanceDao.countById(id);
    }

    /**
     * 通过员工id查询所有考勤记录
     *
     * @param id
     * @return
     */
    @Override
    public List<Attendance> getAttendByEid(Integer id) throws SQLException {
        return attendanceDao.getAttendByEid(id);
    }

    /**
     * 通过员工id查询所有考勤的分页记录
     *
     * @param id
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @Override
    public Page<Attendance> getAttendByEidOfPage(Integer id, long pageNumber, long pageSize) throws SQLException {
        return attendanceDao.getAttendByEidOfPage(id,pageNumber,pageSize);
    }

    /**
     * 获取某时间内员工打卡总数
     *
     * @param date
     * @param id
     * @return
     */
    @Override
    public int countAttByDate(LocalDateTime date, Integer id) throws SQLException {
        return attendanceDao.countAttByDate(date,id);
    }

    /**
     * 查询某员工某时间的打卡记录
     *
     * @param id
     * @param date
     * @return
     */
    @Override
    public Attendance getAttendByEidByDate(Integer id, LocalDateTime date) throws SQLException {
        return attendanceDao.getAttendByEidByDate(id,date);
    }

    /**
     * 查询某员工某时间段内的所有打卡记录（支持分页）
     *
     * @param id
     * @param start
     * @param end
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @Override
    public Page<Attendance> getAttendByEidOfDate(Integer id, LocalDateTime start, LocalDateTime end, long pageNumber, long pageSize) throws SQLException {
        return attendanceDao.getAttendByEidOfDate(id,start,end,pageNumber,pageSize);
    }
}
